package net.hyperz.water.levels.utils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import android.graphics.Bitmap;

public class StationContent {

    public static class Station {

        public String id;
        public String name;

        public Station(String id, String name) {
            this.id = id;
            this.name = name;
        }
        
        @Override
        public String toString() {
            return name;
        }
    }
    
    public static class StationInformation {
    	public String id;
    	public String name;
    	public String state;
    	public String prov;
    	public String latitude;
    	public String longitude;
    	public String regulation_type;
    	public String record_length;
    	public String record_period;
    	public boolean has_real_time;
    	public String body_type;
    	public String ec_office;
    	public Bitmap last_graph;
    	public List<Reading> readings;
    }
    
    public static class Reading {
    	public Date date;
    	public String primary_water_level;
    }
    
    public List<Station> STATIONS = new ArrayList<Station>();
    public Map<String, Station> ITEM_MAP = new HashMap<String, Station>();
    private boolean areLoaded = false;
    
    public static String convertStreamToString(java.io.InputStream is) {
        java.util.Scanner s = new java.util.Scanner(is).useDelimiter("\\A");
        return s.hasNext() ? s.next() : "";
    }
    
    public StationInformation getStationInformation(String id, Document passedIn) {
    	Document stationInfo;
    	StationInformation si = null;
    	stationInfo = passedIn;	//for debugging
    	
    	//check if the disclaimer form was displayed first
    	if (stationInfo == null) {
    		try {
    			stationInfo = HttpLoad.getDocument("http://www.wateroffice.ec.gc.ca/text_search/graph_e.html", "stn", id);
    		} catch (IOException e) {
    			e.printStackTrace();
    		}
    	}
    	
    	if (stationInfo.select("h2:containsOwn(Station Information)").size() > 0) {
    		si = new StationInformation();
    		for (Element row : stationInfo.select("div#topInfo").select("div.row:not(div.mainCol)")) {
    			mapStationInformation(si, row.select("div.leftCol"));
    			mapStationInformation(si, row.select("div.rightCol"));
    		}
    		
    		for (Element row : stationInfo.select("div#bottomInfo").select("div.row:not(div.mainCol)")) {
    			mapStationInformation(si, row.select("div.leftCol"));
    			mapStationInformation(si, row.select("div.rightCol"));
    		}
    	}
    	
		return si;
    }
    
    private void mapStationInformation(StationInformation si, Elements dataList) {
    	Element data;
    	
    	if (dataList.size() > 0) {
    		data = dataList.get(0);
    		
	    	if (data.child(0).text().toLowerCase().contains("active or discontinued")) {
	    		si.state = data.child(1).text();
	    	} else if (data.child(0).text().toLowerCase().contains("province / territory")) {
	    		si.prov = data.child(1).text();
	    	} else if (data.child(0).text().toLowerCase().contains("latitude")) {
	    		si.latitude = data.child(1).text();
	    	} else if (data.child(0).text().toLowerCase().contains("longitude")) {
	    		si.longitude = data.child(1).text();
	    	} else if (data.child(0).text().toLowerCase().contains("gross drainage area")) {
	    		//
	    	} else if (data.child(0).text().toLowerCase().contains("regulation type")) {
	    		si.regulation_type = data.child(1).text();
	    	} else if (data.child(0).text().toLowerCase().contains("record length")) {
	    		si.record_length = data.child(1).text();
	    	} else if (data.child(0).text().toLowerCase().contains("period of record")) {
	    		si.record_period = data.child(1).text();
	    	} else if (data.child(0).text().toLowerCase().contains("real-time data available")) {
	    		si.has_real_time = (data.child(1).text().toUpperCase() == "YES") ? true : false; 
	    	} else if (data.child(0).text().toLowerCase().contains("sediment data available")) {
	    		//
	    	} else if (data.child(0).text().toLowerCase().contains("type of water body")) {
	    		si.body_type = data.child(1).text();
	    	} else if (data.child(0).text().toLowerCase().contains("rhbn")) {
	    		//
	    	} else if (data.child(0).text().toLowerCase().contains("ec regional office")) {
	    		si.ec_office = data.child(1).text();
	    	} else if (data.child(0).text().toLowerCase().contains("data contributed by")) {
	    		//
	    	} else if (data.child(0).text().toLowerCase().contains("datum of published data")) {
	    		//
	    	}
    	}
    }
    
    public List<Station> getStations(Document passedIn) {
    	Document regionList;
    	regionList = passedIn;
    	
    	if (!areLoaded) {
    		if (regionList == null) {
		    	//load list of items, current default to BC
		    	try {
					regionList = HttpLoad.getDocument("http://www.wateroffice.ec.gc.ca/text_search/search_e.html", "search_by", "p", "region", "BC");
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
    		}
    		
    		//do something with the document
    		for (Element row : regionList.select("table#search_results_table").select("tr:has(td)")) {
    			addItem(new Station(row.child(1).child(0).text(), row.child(0).child(0).text()));
    		}
    	}
    	
    	return STATIONS;
    }
    
    private void addItem(Station item) {
        STATIONS.add(item);
        ITEM_MAP.put(item.id, item);
    }
}
